package com.activity.activitybpm.controller.login;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import com.activity.activitybpm.controller.login.param.LoginParam;
import com.activity.activitybpm.entity.Account;
import com.activity.activitybpm.service.AccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户登录-API
 */
@RestController
@RequestMapping(value = "/account", produces = "application/json;charset=UTF-8")
public class LoginController {

    @Resource
    private AccountService accountService;

    /**
     * 登录接口
     * @param loginParam
     * @return
     */
    @PostMapping(value = "/dologin", produces = "application/json;charset=UTF-8")
    public SaResult login(@RequestBody LoginParam loginParam) {
        System.out.println("===============用户登录===============");
        // 校验账号密码
        Account account = accountService.getAccountUserInfo(loginParam.getAccount(), loginParam.getPassword());
        if (ObjectUtil.isNull(account)) {
            return SaResult.ok("登录失败！账号密码异常，请重新再试");
        }
        // 登录创建会话标识token
        StpUtil.login(account.getAccount());
        return SaResult.ok(StpUtil.getTokenInfo().toString());
    }

    /**
     *
     * @return
     */
    @GetMapping(value = "/checkLogin", produces = "application/json;charset=UTF-8")
    public SaResult checkLogin() {
        return SaResult.ok("是否登录" + StpUtil.isLogin());
    }

    /**
     * 退出接口
     * @return
     */
    @PostMapping(value = "/logout", produces = "application/json;charset=UTF-8")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok("退出登录......");
    }
}
